package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
@GetMapping
    public Result list(UserDto userDto){
        log.info("用户查询列表:{}",userDto);
         PageResult<User> pageResult = userService.list(userDto);
        return Result.success(pageResult);
    }
//    新增用户
@PostMapping
    public Result add(@RequestBody User user){
        log.info("用户添加:{}",user);
        userService.add(user);
        return Result.success();
    }

//    删除用户
    @DeleteMapping("/{ids}")
    public Result deleteById(@PathVariable List<Integer> ids){
        log.info("用户删除:{}",ids);
        userService.deleteById(ids);
        return Result.success();
    }

//    根据ID查询
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
//     System.out.println("查询id:"+id);
        log.info("查询id:{}",id);
        User user = userService.getUserById(id);
        return Result.success(user);
    }

//    修改用户
    @PutMapping
    public Result update(@RequestBody User user){
        log.info("用户修改:{}",user);
        userService.update(user);
        return Result.success();
    }


//    根据用户角色标识查询用户信息
    @GetMapping("/role/{roleLabel}")
    public Result selectByRoleLabel(@PathVariable( "roleLabel") String label){
    log.info("根据用户角色标识查询用户信息:{}",label);
        List<User> users = userService.selectByRoleLabel(label);
        return Result.success(users);
    }
//    查询所有用户
    @GetMapping("/list")
    public Result select(){
    log.info("查询所有用户");
    List<User> userList = userService.selectAll();
    return Result.success(userList);
    }

//    根据部门查询 用户
    @GetMapping("/dept/{deptId}")
    public Result selectByDeptId(@PathVariable Integer deptId){
    log.info("根据部门查询用户:{}",deptId);
    List<User> users = userService.selectByDeptId(deptId);
    return Result.success(users);
    }
}
